//
//  StatuesTableViewTopView.swift
//  swift-GWYWeiBo
//
//  Created by x on 2017/6/2.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

class StatuesTableViewTopView: UIView {

    /// 数据模型
    var modle:Status? {
        didSet {
            //给子控件赋值
            //头像地址可能有可能没有
            if (modle?.user?.imageURL) != nil {
                u_headImage.sd_setImage(with: (modle?.user?.imageURL)!)//头像
            }
            //设置认证图标
            if modle?.user?.verifiedImage != nil {
                u_verifiedImage.image = (modle?.user?.verifiedImage)!
            }
            //设置会员等级图标
            if modle?.user?.mbrankImage != nil {
                u_memberImage.image = (modle?.user?.mbrankImage)!
            }
            u_nickLable.text = modle?.user?.name//昵称
            u_wb_timeLable.text = modle?.created_at//时间
            u_wb_sourceLable.text = modle?.source//来源
            
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //初始化UI
        steupUI()
    }
    
    func steupUI() -> Void {
        translatesAutoresizingMaskIntoConstraints = false
        //1.添加子控件
        addSubview(u_headImage)
        addSubview(u_nickLable)
        addSubview(u_verifiedImage)
        addSubview(u_memberImage)
        addSubview(u_wb_timeLable)
        addSubview(u_wb_sourceLable)

        //2.布局子控件
        //2.1头像添加约束
        addConstraint(NSLayoutConstraint.init(item: u_headImage, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.top, multiplier: 1.0, constant: 10))
        addConstraint(NSLayoutConstraint.init(item: u_headImage, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.left, multiplier: 1.0, constant: 10))
        u_headImage.addConstraint(NSLayoutConstraint.init(item: u_headImage, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 50))
        u_headImage.addConstraint(NSLayoutConstraint.init(item: u_headImage, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 50))
        //2.2认证图标添加约束
        addConstraint(NSLayoutConstraint.init(item: u_verifiedImage, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: u_headImage, attribute: NSLayoutAttribute.right, multiplier: 1.0, constant: -15))
        addConstraint(NSLayoutConstraint.init(item: u_verifiedImage, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: u_headImage, attribute: NSLayoutAttribute.bottom, multiplier: 1.0, constant: 0))
        u_verifiedImage.addConstraint(NSLayoutConstraint.init(item: u_verifiedImage, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 14))
        u_verifiedImage.addConstraint(NSLayoutConstraint.init(item: u_verifiedImage, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 14))
        
        //2.3昵称添加约束
        addConstraint(NSLayoutConstraint.init(item: u_nickLable, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: u_headImage, attribute: NSLayoutAttribute.right, multiplier: 1.0, constant: 10))
        addConstraint(NSLayoutConstraint.init(item: u_nickLable, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: u_headImage, attribute: NSLayoutAttribute.top, multiplier: 1.0, constant: 5))
        //2.4 会员图标添加约束
        addConstraint(NSLayoutConstraint.init(item: u_memberImage, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: u_nickLable, attribute: NSLayoutAttribute.top, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint.init(item: u_memberImage, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: u_nickLable, attribute: NSLayoutAttribute.right, multiplier: 1.0, constant: 5))
        
        u_memberImage.addConstraint(NSLayoutConstraint.init(item: u_memberImage, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 14))
        u_memberImage.addConstraint(NSLayoutConstraint.init(item: u_memberImage, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 14))
        //2.5 微博时间添加约束
        addConstraint(NSLayoutConstraint.init(item: u_wb_timeLable, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: u_headImage, attribute: NSLayoutAttribute.bottom, multiplier: 1.0, constant: -5))
        addConstraint(NSLayoutConstraint.init(item: u_wb_timeLable, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: u_nickLable, attribute: NSLayoutAttribute.left, multiplier: 1.0, constant: 0))
        //2.6 微博来源添加约束
        addConstraint(NSLayoutConstraint.init(item: u_wb_sourceLable, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: u_wb_timeLable, attribute: NSLayoutAttribute.right, multiplier: 1.0, constant: 5))
        addConstraint(NSLayoutConstraint.init(item: u_wb_sourceLable, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: u_wb_timeLable, attribute: NSLayoutAttribute.bottom, multiplier: 1.0, constant: 0))

    }
    /// 返回顶部视图的高度
    ///
    /// - Returns: <#return value description#>
    func topViewHeight() -> CGFloat {
        return u_headImage.frame.maxY + 10
    }
    //MARK: -- 懒加载
    ///用户头像
    private lazy var u_headImage = { () -> UIImageView in
        
        let headImage = UIImageView.creatUIImageView(placeholderImage: "avatar_default")
        headImage.layer.cornerRadius = 25;
        headImage.layer.masksToBounds = true
        return headImage
    }()
    ///用户昵称
    private lazy var u_nickLable = UILabel.creatLbale(color: UIColor.orange, fontSize: 15)
    ///用户认证图标
    private lazy var u_verifiedImage = UIImageView.creatUIImageView(placeholderImage: "")
    ///用户会员图标
    private lazy var u_memberImage = UIImageView.creatUIImageView(placeholderImage: "")
    
    ///发微博时间
    private lazy var u_wb_timeLable = UILabel.creatLbale(color: UIColor.gray, fontSize: 12)
    ///微博来源
    private lazy var u_wb_sourceLable = UILabel.creatLbale(color: UIColor.gray, fontSize: 12)

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
